# coding: utf-8
from flask_sqlalchemy import SQLAlchemy
from flask_login import current_user
from config import app
from db.utils import db, UserMixin

# db = SQLAlchemy(app)



class Book(db.Model):
    __tablename__ = 'book'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), nullable=False, info='书名')
    book_type_id = db.Column(db.Integer, nullable=False, info='图书类型ID')
    author = db.Column(db.String(20), nullable=False, info='作者')
    price = db.Column(db.Numeric(10, 2), nullable=False, info='价格')
    is_active = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='是否上架 0-否;1-是')
    available_count = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    is_delete = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='是否删除 0-否;1-是')



class BootType(db.Model):
    __tablename__ = 'boot_type'

    id = db.Column(db.Integer, primary_key=True)
    type_name = db.Column(db.String(20), nullable=False, info='类型名')
    type_key = db.Column(db.String(20), nullable=False, info='类型关键字')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())



class BorrowBook(db.Model):
    __tablename__ = 'borrow_book'

    id = db.Column(db.Integer, primary_key=True)
    book_id = db.Column(db.Integer, nullable=False, info='图书ID')
    book_name = db.Column(db.String(20), nullable=False, info='书名')
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    username = db.Column(db.String(20), nullable=False, info='用户名')
    card_id = db.Column(db.String(20), nullable=False, info='借阅证ID')
    borrow_time = db.Column(db.DateTime, nullable=False, info='借书时间')
    return_time = db.Column(db.DateTime, info='还书时间')
    is_overdue = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='是否超期 0-否;1-是')
    overdue_days = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    overdue_fee = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())
    is_pay_overdue = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='是否支付超期费 0-否;1-是')
    is_return = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='是否还书 0-否;1-是')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())



class BorrowCard(db.Model):
    __tablename__ = 'borrow_card'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, info='借阅证持有者用户ID')
    card_id = db.Column(db.String(20), nullable=False, server_default=db.FetchedValue(), info='借阅证ID')
    valid_until = db.Column(db.DateTime, nullable=False, info='借阅证有效期')
    borrow_limit = db.Column(db.Integer, nullable=False, info='借阅上限')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())



class SysPerm(db.Model):
    __tablename__ = 'sys_perm'

    id = db.Column(db.Integer, primary_key=True)
    perm_name = db.Column(db.String(40), nullable=False, info='权限名')
    perm_key = db.Column(db.String(40), nullable=False, info='权限关键字')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())



class SysRole(db.Model):
    __tablename__ = 'sys_role'

    id = db.Column(db.Integer, primary_key=True)
    role_name = db.Column(db.String(40), nullable=False, info='角色名')
    role_key = db.Column(db.String(40), nullable=False, info='角色关键字')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())



class SysRolePerm(db.Model):
    __tablename__ = 'sys_role_perm'

    id = db.Column(db.Integer, primary_key=True)
    role_id = db.Column(db.Integer, nullable=False, info='角色ID')
    role_key = db.Column(db.String(40), nullable=False, info='角色关键字')
    perm_id = db.Column(db.Integer, nullable=False, info='权限ID')
    perm_key = db.Column(db.String(40), nullable=False, info='权限关键字')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())



class SysUserRole(db.Model):
    __tablename__ = 'sys_user_role'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    username = db.Column(db.String(40), nullable=False, info='用户关键字')
    role_id = db.Column(db.Integer, nullable=False, info='角色ID')
    role_key = db.Column(db.String(40), nullable=False, info='角色关键字')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())



class User(db.Model, UserMixin):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), nullable=False, info='用户名')
    password = db.Column(db.String(100), nullable=False, info='密码')
    user_type = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='用户类型: 0-普通用户;1-图书管理员;2-系统管理员')
    age = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='年龄')
    gender = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='性别: 0-男;1-女')
    birthday = db.Column(db.Date, info='生日')
    user_idcard = db.Column(db.String(20), info='身份证ID')
    user_phone = db.Column(db.String(20), info='手机号')
    borrow_card_apply_status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='借阅证申请状态 -1:申请失败;0-未申请;1-申请中;2-已申请')
    bank_account = db.Column(db.String(20), info='银行卡号')
    is_active = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='是否激活 0-否;1-是')
    create_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    update_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    is_delete = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info=' 0-;1-')
